package com.suliao.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suliao.common.annotation.LoginPass;
import com.suliao.common.base.PageFactory;
import com.suliao.common.base.Result;
import com.suliao.common.base.UserContext;
import com.suliao.common.util.SessionUtil;
import com.suliao.home.entity.industry.ExhibitionBannerDO;
import com.suliao.home.entity.industry.InfoDownloadDO;
import com.suliao.home.service.industry.ExhibitionBannerService;
import com.suliao.home.service.industry.InfoDownloadService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：FeifeiYu
 * @date ：Created in 2020/12/21 17:05
 * @description：展厅资料下载
 * @version: 1.0
 */

@Controller
@RequestMapping(value = "/download")
@LoginPass
public class ExhibitDownloadController {


    /**
     * 展厅banner 服务类
     */
    @Reference
    private ExhibitionBannerService exhibitionBannerService;

    /**
     * 下载资料表 服务类
     */
    @Reference
    private InfoDownloadService infoDownloadService;

    /**
     * 共享工具类
     */
    @Autowired
    private SessionUtil sessionUtil;


    /**
     * 通过类型分页查询下载资料
     *
     * @param mv
     * @param page
     * @param companyId
     * @param type 1:展品资料 2:公司资料 3:其他资料
     * @return
     */
    @GetMapping(value = "downloadlist")
    public String ExhibitAllDownloadlist(Model mv, Integer page, Integer companyId,Integer type) {
        try {
            //分页
            Integer pageSize = 8;
            if (page == null || page < 1) {
                page = 1;//显示首页
                pageSize = 8;//每页8项
            }

            //获取"资料下载"的banner信息
            String position = "资料下载";
            List<ExhibitionBannerDO> bannerList = exhibitionBannerService.getExhibitionHallbanner(companyId, position);
            Integer pflag = 0;
            if (bannerList.size() > 0) {
                pflag = 1;
            } else {
                pflag = 0;
            }
            mv.addAttribute("pflag", pflag);
            mv.addAttribute("bannerList", bannerList);

            //通过公司id和资料分类分页查询“下载资料表”信息
            Page<InfoDownloadDO> downloadList = infoDownloadService.getExhibitDownloadList(PageFactory.createPageInfo(page, pageSize), companyId, type);
            //当前页面
            mv.addAttribute("nowPage", page);
            //总记录数
            mv.addAttribute("totalNum", downloadList.getTotal());
            //每页显示多少条
            mv.addAttribute("pageS", downloadList.getSize());
            //封装下载资料表数据
            mv.addAttribute("downloadList", downloadList.getRecords());
            //查询推荐下载
            List<InfoDownloadDO> recommendDownLoad = infoDownloadService.queryRecommendDownLoad(companyId);
            mv.addAttribute("recommendDownLoad", recommendDownLoad);

            mv.addAttribute("companyId", companyId); //公司id
            mv.addAttribute("banPositionBack", null);
            mv.addAttribute("productTitle", null);
            mv.addAttribute("flag", type);

            // 获取登录人信息
            UserContext user = sessionUtil.getUserContext();
            mv.addAttribute("userinfo", user);

            //查询登录用户的塑金余额
            if (user != null) {
                Integer corpId = user.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (corpId == null||corpId==0) {
                    mv.addAttribute("plasticGoldList", plasticGoldList);
                } else {
                    //plasticGoldList = industryExhibitorService.findAllPlasticGolds(corpId);
                    mv.addAttribute("plasticGoldList", plasticGoldList);
                }
            }
            mv.addAttribute("hallName", "资料下载");
            mv.addAttribute("banName", "资料下载");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "exhibitHall/exhibit_download";
    }


    /**
     * 通过id更新资料下载量
     * @param id
     * @return
     */
    @PostMapping(value = "updateNumber")
    @ResponseBody
    public Result updateNumber(@RequestParam(value = "id") Integer id) {
        Result result = null;
        try {
            Integer res = infoDownloadService.updateDownLoadNumber(id);
            if(res>0){
                result = Result.ok();
            }else {
                result = Result.error(null);
            }
        } catch (Exception e) {
            result = Result.error("服务器异常");
            e.printStackTrace();
        }
        return result;
    }

}
